﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace DBLayer
{
    class AccessDBSQLClient
    {
        public static readonly string connectionString = "Data Source=balder.ucn.dk; Initial Catalog=DM72_5; User ID=dm72_5; Password=MaaGodt; MultipleActiveResultSets=True;";
        public static readonly SqlConnection dbconn = new SqlConnection(connectionString);

        private static SqlCommand dbCmd;

        private static void Open()
        {
            if (dbconn.State.ToString().CompareTo("Open") != 0)
                dbconn.Open();
        }

        public static void Close()
        {
            dbconn.Close();
        }

        public static SqlCommand GetDbCommand(string sql)
        {
            // Problemer med kun at have 1 connection åbenbart
            SqlConnection conn = new SqlConnection(connectionString);
            conn.Open();
            dbCmd = new SqlCommand(sql, conn);

            dbCmd.CommandText = sql;
            return dbCmd;

            //if (dbconn.State.ToString().CompareTo("Open") != 0)
            //    Open();
            //if (dbCmd == null)
            //{
            //    dbCmd = new SqlCommand(sql, dbconn);
            //}
            //dbCmd.CommandText = sql;
            //return dbCmd;
        }

        public static int ExecuteQuery(string sql)
        {
            int rc = -1;
            try
            {
                dbCmd = GetDbCommand(sql);
                rc = dbCmd.ExecuteNonQuery();
            }
            catch (SqlException exp)
            {
                // Do nothing using return codes
                Console.WriteLine(exp.Message);
                Console.ReadLine();
            }
            finally
            {
                dbCmd.Connection.Close();
                //if (dbconn != null)
                //{
                //    Close();
                //}
            }

            return rc;
        }

       
    }
}
